﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GempGame
{
    public class BankLoan
    {
        public double value {get; set;}
        public double paid { get; set; }
        public int week { get; set; }


        public BankLoan()
        {
            this.week = 0;
            this.value = 0.0;
            this.paid = 0.0;
        }
        public void setPaid(double v)
        {
            this.paid = this.paid + v;
        }
    }

    public class Bank
    {
        public List<BankLoan> loans { get; set; }
        public double max { get; set; }
        public double left { get; set; }

        public Bank(double m)
        {
            this.max = m;
            this.left = this.max;
            this.loans = new List<BankLoan>();
        }

        public BankLoan getLoan(int week)
        {
            foreach (BankLoan bl in this.loans)
            {
                if (bl.week == week)
                    return bl;
            }
            BankLoan b = new BankLoan();
            b.week = -1;
            return b;
        }

        public bool newLoan(double val, int week)
        {
            if (val <= left)
            {
                if (this.loans.Count > 0)
                {
                    if (this.loans[this.loans.Count - 1].week != week)
                    {
                        BankLoan bL1 = new BankLoan();
                        bL1.value = val;
                        bL1.paid = 0.0;
                        bL1.week = week;
                        this.left -= val;
                        this.loans.Add(bL1);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    BankLoan bL = new BankLoan();
                    bL.value = val;
                    bL.paid = 0.0;
                    bL.week = week;
                    this.left -= val;
                    this.loans.Add(bL);
                    return true;
                }
            }
            return false;
        }

        //VERIFICAR
        public double payLoan(double v, int week)
        {
            System.Console.WriteLine("PAYLOAN: " + v + " - " + week);
            if (loans.Count > 0)
            {
                //foreach (BankLoan bl in this.loans)
                for (int i = 0; i < loans.Count; i++ )
                {
                    if (this.loans[i].week == week)
                    {
                        System.Console.WriteLine("  ACHOU WEEK " + this.loans[i].paid + " -> " + this.loans[i].value);
                        if (this.loans[i].paid < this.loans[i].value)
                        {
                            if ((this.loans[i].value - this.loans[i].paid - v) >= 0)
                            {
                                System.Console.WriteLine("      SETPAID: " + v + " - " + this.loans[i].week);
                                this.loans[i].setPaid(v);
                                System.Console.WriteLine("          PAID: " + this.loans[i].paid + " - " + this.loans[i].week);
                                return 0.0;
                            }
                            else
                            {
                                System.Console.WriteLine("      SETPAID: " + (this.loans[i].value - this.loans[i].paid) + " - " + this.loans[i].week);
                                double sobra = (this.loans[i].paid + v) - this.loans[i].value;
                                this.loans[i].setPaid(this.loans[i].value - this.loans[i].paid);
                                System.Console.WriteLine("          PAID: " + this.loans[i].paid + " - " + this.loans[i].week);
                                return sobra;
                            }
                        }
                        else //caso ja esteja pago
                            return -1.0;
                    }
                }
            }
            //caso nao exista essa semana ou nao hajam emprestimos efectuados
            return -2.0;
        }

        public double getTotalDebt()
        {
            double ret = 0.0;
            foreach (BankLoan bl in this.loans)
            {
                ret += (bl.value - bl.paid);
            }
            return ret;
        }

        public string[] weeksLoanList()
        {
            if (this.loans.Count > 0)
            {
                string[] lista = new string[this.loans.Count];
                int i = this.loans.Count - 1;
                foreach (BankLoan bl in this.loans)
                {
                    lista[i] = "Semana " + bl.week;
                    i--;
                }
                return lista;
            }
            return new string[] {"Não há empréstimos"};
        }
    }
}
